package com.home.rest;

import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.home.base.exceptions.ServiceException;
import com.home.base.msg.ObjectRestResponse;
import com.home.base.util.SmsDemo;
import com.home.config.InitDataConfig;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.Collection;

/**
 * @author ：chenxf
 * @date ：Created in 2020/3/20 9:18
 * @description：
 * @modified By：
 * @version: $
 */
@Slf4j
@RestController
@RequestMapping("api")
public class ApiController {
    @Autowired
    protected CacheManager cacheManager;
    String loginUser="18322392919";

    static final String accessKeyId = "LTAI1lUJ2rhS46gJ";
    static final String accessKeySecret = "QqMrkzAHG9cq6zIZK46MzHiZuCWrui";
      Collection<? extends GrantedAuthority> authorities;
    @PostMapping("/login")
    public ObjectRestResponse login(String name, String password){
        if(StringUtils.isEmpty(password)){
            throw new ServiceException("验证码不能为空");
        }
       // checkSmsCode(name,password);
        UsernamePasswordAuthenticationToken upToken = new UsernamePasswordAuthenticationToken(name, password, authorities); // 进行安全认证
        upToken.setDetails(name);
        SecurityContextHolder.getContext().setAuthentication(upToken);
        return  new ObjectRestResponse("登陆成功").rel(true).result(name);
    }

    @PostMapping("/sendText")
    public ObjectRestResponse sendTextI() {
        String veryCode = RandomStringUtils.randomNumeric(4);
        InitDataConfig.setVeryCode(veryCode);
        //log.info("veryCode:" + veryCode);
        // 发送验证码
        try {
            SendSmsResponse response;
                response = SmsDemo
                        .sendSms(loginUser, "号码库","SMS_186510480",veryCode,accessKeyId,accessKeySecret);

            log.debug("result={}", response.getMessage());
            if (!"OK".equals(response.getCode())) {
                throw new ServiceException("发送失败：" + response.getMessage());
            }

        } catch (ClientException e) {
            log.error(e.getErrMsg());
        }
        return new ObjectRestResponse().rel(true).msg("发送成功");

    }

    protected void  checkSmsCode(String phone,String code){

        String veryCode = "aaaa1111";

        if ( !veryCode.equals(code)) {
            throw new ServiceException("密码错误");
        }
    }

}
